package com.credithc.signature.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.ro.manager.ChannelRo;
import com.credithc.signature.common.service.ChannelService;
import com.credithc.signature.common.vo.manager.ChannelVo;
import com.credithc.signature.manager.service.AuthorizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 描 述: 渠道信息Controller<br/>
 * 作 者: JingBo Hu<br/>
 * 创 建：2019/5/24 16:55<br/>
 * 版 本：v1.0.0<br>
 */
@RestController
@RequestMapping("/manager/scs/v2")
@Slf4j
public class ChannelController {

    @Autowired
    MessageFactory messageFactory;

    @Autowired
    ChannelService channelService;

    @Autowired
    AuthorizationService authorizationService;


    /**
     * 查询所有
     *
     * @param channelRo
     * @return
     */
    @PostMapping("/channel/list")
    public Message queryAll(@RequestHeader("token") String token, @RequestBody @Valid ChannelRo channelRo, HttpServletRequest request) throws Exception {
        try {
            ChannelRo newChannelRo = null;
            try {
                newChannelRo = authorizationService.authChannelRo(channelRo, token, request);
            } catch (Exception e) {
                return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, e.getMessage());
            }
            PageInfo<ChannelVo> pageInfo = channelService.queryAll(newChannelRo);
            Map<String, Object> map = new HashMap<>(16);
            map.put("pageInfo", pageInfo);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (Exception e) {
            log.error("获取授权时发生异常", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "获取授权时发生异常");
        }
    }

    /**
     * 不鉴权查询
     *
     * @param token
     * @param channelRo
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/channel/noAuth/list")
    public Message queryAllNoAuth(@RequestHeader("token") String token, @RequestBody @Valid ChannelRo channelRo, HttpServletRequest request) throws Exception {
        try {
            PageInfo<ChannelVo> pageInfo = channelService.queryAllNoAuth(channelRo);
            Map<String, Object> map = new HashMap<>(16);
            map.put("pageInfo", pageInfo);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (Exception e) {
            log.error("获取授权时发生异常", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "获取授权时发生异常");
        }
    }

    /**
     * 新增--对外提供
     *
     * @param channelRo
     * @return
     */
    @PostMapping("/channel/save")
    public Message save(@RequestBody @Valid ChannelRo channelRo) {
        try {
            channelService.save(channelRo);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, "渠道信息保存成功！");
        } catch (Exception e) {
            log.error("对外提供的渠道保存接口发生异常", e);
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道信息保存失败: ", e.getMessage());
        }
    }

}
